package com.thti.mecool.client.widget;

import com.thti.mecool.client.activity.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Checkable;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CheckView extends LinearLayout implements Checkable{
	private View view;
	private View checkView;
	private Context context;
	private ImageView iv;
	private boolean isChecked=false;
	private IsCheckListener isCheckListener;
	private int color=-1;
	public CheckView(Context context) {
		super(context);
		this.context = context;
		init(context);
	}

	public CheckView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		init(context);
	}

	private void init(Context context) {
		setOrientation(0);
		if(view!=null)
			addView(view);
		checkView=LayoutInflater.from(context).inflate(R.layout.mycheckbox, null);
		setGravity(Gravity.CENTER_VERTICAL);
		isChecked=false;
		iv=(ImageView) checkView.findViewById(R.id.check);
	}
	public void setText(CharSequence cs){
		TextView tv=new TextView(context);
		tv.setText(cs);
		addView(tv);
	}
	public CheckView initCheck(){
		addView(checkView);
		return this;
	}
	public void AddIsCheckListener(IsCheckListener isCheckListener){
		this.isCheckListener=isCheckListener;
	}
	public CheckView setView(View view) {
		this.view = view;
		addView(view);
		return this;
	}

	public void setView(int viewId) {
		view = LayoutInflater.from(context).inflate(viewId, null);
		addView(view);
	}

	@Override
	public boolean isChecked() {
		return isChecked;
	}

	@Override
	public void setChecked(boolean checked) {
		this.isChecked=checked;
	}

	@Override
	public void toggle() {
		setChecked(!isChecked());
		if(isChecked()){
			if(color!=-1)
				setBackgroundColor(color);
			iv.setImageResource(R.drawable.finish);
			}
		else{
			if(color!=-1) 
				setBackgroundColor(android.R.color.transparent);
			iv.setImageResource(android.R.color.transparent);
			}			
		if(isCheckListener!=null)
		isCheckListener.isCheck(isChecked());
	}
	public interface IsCheckListener{
		public void isCheck(boolean checked);
	}
	public void setCheckImg(int id) {
		iv.setImageResource(id);
	}
	
	public void setOnClickCheckBackgroundColor(int color){
		this.color=color;
	}
}
